#include <stdio.h>
//#include<malloc.h>
#include <stdlib.h>
#include<string.h>
//void Pfree(struct Student  *PStudent);
typedef struct Student
{    	  	  	  
    int ID;
    char name[10];
    float score;
    struct Student *pNextNode;
}    	  	  	   Student, *PStudent;
 
//建立节点
PStudent ByeNode(int _ID, char _name[], float _score) 
{    	  	  	  
    PStudent pNewNode = (PStudent)malloc(sizeof(struct Student));   //1
    if (NULL != pNewNode)         //1  
    {    	  	  	  
        pNewNode->ID = _ID;           
        strcpy(pNewNode->name , _name);         
        pNewNode->score = _score;           
        pNewNode->pNextNode = NULL;     //1  
    }
    return pNewNode;            
}    	  	  	  
 
// 尾插法建立单链表
void InsertTail(PStudent* pHead, int _ID, char _name[], float _score)//2
{    	  	  	  
    PStudent pNode = NULL; //1
    PStudent pNewNode = NULL;
    //  assert(pHead);
    if (NULL == (*pHead))     //1
    {    	  	  	  
        *pHead = ByeNode(_ID, _name, _score); //1
    }
    else
    {    	  	  	  
        pNode = *pHead;
        while (NULL != pNode->pNextNode)     //1
        {    	  	  	  
            pNode = pNode->pNextNode;    
        }
        pNewNode = ByeNode(_ID, _name, _score); //1
        pNode->pNextNode = pNewNode;    
    }
}    	  	  	  
void SortList(PStudent pHead)       //2
{    	  	  	  
    int IDTemp;
    char nameTemp[10];
    float scoreTemp;
    int flag = 0;
    PStudent pTailNode = NULL;     //1
 
 
    if (NULL == pHead)          //1
    {    	  	  	  
        return ;
    }
    else
    {    	  	  	  
        flag = 0;
        pTailNode = NULL;
        while (pTailNode != pHead)  //1
        {    	  	  	  
            PStudent pPreNode = pHead;   //1
            while (pPreNode->pNextNode != pTailNode)    //1
            {    	  	  	  
                PStudent pCurNode = pPreNode->pNextNode;  //1
                if (pPreNode->score < pCurNode->score)
                {    	  	  	  
                    IDTemp = pPreNode->ID;
                    strcpy(nameTemp , pPreNode->name);
                    scoreTemp = pPreNode->score;
 
                    pPreNode->ID = pCurNode->ID;
                    strcpy(pPreNode->name , pCurNode->name);
                    pPreNode->score = pCurNode->score;
 
                    pCurNode->ID = IDTemp;
                    strcpy(pCurNode->name, nameTemp);
                    pCurNode->score = scoreTemp;
 
                    flag = 1;
                }
                pPreNode = pPreNode->pNextNode;//1
            }
            if (0 == flag)
            {    	  	  	  
                break;
            }
            pTailNode = pPreNode;    //1
        }
    }
}    	  	  	  
 
//从头到尾打印单链表
void PrintList(PStudent pHead, int n)  //2
{    	  	  	  
    int count = 0;
    PStudent tempPnode = NULL;        //1
    tempPnode = pHead;         //1
    if (NULL != pHead)
    {    	  	  	  
        while (tempPnode != NULL)   //1
        {    	  	  	  
            if (count < n)
            {    	  	  	  
                printf("%-5d  %-8s  %-4.2f\n", tempPnode->ID, tempPnode->name, tempPnode->score);
                tempPnode = tempPnode->pNextNode;     //1
                count++;
            }
            else if (count == n)
            {    	  	  	  
                break;
            }
        }
    }
}    	  	  	  
void Pfree(struct Student *PStudent)//2
{    	  	  	  
    struct Student *p, *q; //1
    p = PStudent;
    while (p->pNextNode != NULL) //1
    {    	  	  	  
        q = p->pNextNode;
        p->pNextNode = q->pNextNode;//1
        free(q);//1
    }
    free(p);
}    	  	  	  
 
int main(void)                
{    	  	  	  
    PStudent pList1 = NULL;
    PStudent tempPnode = NULL;        //1
    int i = 0;
    int tempID = 0;
    char tempname[10];
    float tempscore;
    int n = 0;
 
    printf("请输入学生人数：\n");
    scanf("%d", &n);          //1
 
    for (i = 0; i < n; i++)  //1
    {    	  	  	  
        printf("请输入第%d个学生的ID、姓名和成绩:", i + 1);
        scanf("%d %s %f", &tempID, tempname, &tempscore);     //1
        //  printf("%d %s %f\n", tempID, tempname, tempscore);
        InsertTail(&pList1, tempID, tempname, tempscore);                   //1
    }
 
    printf("所有学生的成绩：\n");
    printf("ID     姓名      成绩\n");
    PrintList(pList1, n);                        //1
    SortList(pList1);                            //1
    printf("前三名学生的成绩:\n");
    printf("ID     姓名      成绩\n");
    PrintList(pList1, 3);
    Pfree(pList1);                                //1
    return 0;
}